

define(['vue','moment','config','common','../model/upLoadList','../model/downLoadList'],
	function(Vue,moment,config,utils,upLoadList,downLoadList){
	'use strict';

	var recommendVue = new Vue({
		el:'#app',
		data:{
			parentUserCode:'',	//上级推荐人编码	
			list:'',	
			page: 1,
			totalPage:0,
			isLoadMore:0,
			pageSize:'',
			totalSize: 0
		},
		filters: {
		  moment: function (date) {
		    return moment(date).format('YYYY-MM-DD');
		  }
		 
		},
		watch:{
			list: function(to,from){
				if(to.length == 0){
					$('.error404').show();
				}
			}
		},
		created:function(){	
			var _this=this;
			//_this.pageSize =  parseInt(Math.ceil(($('body').height()-$('.headTitle').height()-$('.title').height())/12), 10);
			_this.pageSize = 30;
			var data = {
				page: _this.page,
				pageSize: _this.pageSize
			}
			upLoadList.ajax(utils.getWebUrl('recommend/list'),data,function(res){
				_this.totalPage = res.data.totalPage;
				_this.parentUserCode = res.data.parentUserCode;
				_this.list = res.data.list;
				_this.totalSize = res.data.total;
			})
		},
		directives: {
		  scroll: {
		    bind: function (el, binding){
		      window.addEventListener('scroll',function() {
		       		var fnc = binding.value;   
   					fnc();
		      })
		    }
		  }
		},
		watch:{
			list: function(to,from){
				if(to.length == 0 && this.list !== ""){
					$('.error404').show();
				}else{
					$('.error404').hide();
				}
			}
		},
		//判定事件
		methods:{
			loadMore:function(event){
				var _this = this;
				if ((upLoadList.getScrollTop() + upLoadList.getClientHeight() >= upLoadList.getScrollHeight())&&_this.page<_this.totalPage) {
					_this.page = _this.page+1;
		           	var data = {
						page: _this.page,
						pageSize: _this.pageSize
					}
					
					upLoadList.ajax(utils.getWebUrl('recommend/list'),data,function(res){
						_this.list = _this.list.concat(res.data.list);
						_this.totalPage = res.data.totalPage;
					})
		        }
			},
			Start:function(event){

				var touch = event.targetTouches[0];
				this.start = touch.pageY;

			},

			Move:function(event){
				var touch = event.targetTouches[0];
				this.end = (this.start - touch.pageY);
				var slideDown1 = $('#pullDown1');
				//下滑才执行操作 
                if(this.end < 0){
                	
                	slideDown1.text('下拉刷新  ↓');
	        		slideDown1.show();
	        		slideDown1[0].style.height =  (parseInt("0px")-this.end) + "px";
	        		if(this.end < -48){
	        			/*slideDown1.hide(); 
	        			downLoadList.slideDownStep1(this.end);*/
	        			slideDown1.text('松手开始更新  ↑')
	        		}
                }else{
                	slideDown1.hide();
                }
			},

			End:function(event){
				var _this = this;
				var slideDown1 = $('#pullDown1');
				if(this.end >0){  
					slideDown1.hide();
	            }else{ 

	                if(this.end < -48){
	                	var slideDown1 = $('#pullDown1');
	                	var slideDown2 = $('#pullDown2');
						//$('.pullDownLabel').text("下拉刷新...");
						slideDown1.hide();
				        slideDown2.show();
				        _this.list = '';
	                	downLoadList.slideDownStep2(); 
		                //刷新成功则 
		                //模拟刷新成功进入第三步
		                _this.page = 1;
		                var data = {
							page: _this.page,
							pageSize: _this.pageSize
						}
						$('#pullDown3').hide();
		                utils.ajax(utils.getWebUrl('recommend/list'),data,
		                	'POST',
		                	function(res){
								_this.totalPage = res.data.totalPage;
								_this.parentUserCode = res.data.parentUserCode;
								_this.list = res.data.list;
						},null,function(){
							downLoadList.slideDownStep3();
						})
		                
	                }else{
	                	var slideDown1 = $('#pullDown1');
		        		slideDown1.hide();
	                }
	            }
			},
		}
		
	});


	return recommendVue
});